// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Define useful soy related js_test functions.
 *
 * @author jasonstredwick@google.com (Jason Stredwick)
 */


goog.provide('bite.common.test_framework.SoyTests');



/**
 * Used to execute tests.
 * @constructor
 */
bite.common.test_framework.SoyTests = function() {};
goog.addSingletonGetter(bite.common.test_framework.SoyTests);


/**
 * Examines all tag attribute values within the string generated by the soy
 * template against an expected value.  The test is an assert chosen by the
 * user.
 * @param {function(string, string)} assert The assert function used to
 *     test the attribute values against the expected value.
 * @param {string} expectedValue The value to compare against.
 * @param {string} tag The attribute tag to search for.
 * @param {function(Object=):string} template The soy template function.
 * @param {Object=} data The data to pass to the template.
 */
bite.common.test_framework.SoyTests.prototype.testAttributeValue =
    function(assert, expectedValue, tag, template, data) {
  var regexp = RegExp(tag + '=["]([^"]*)["]', 'gi');
  var string = template(data || {});

  // Keep executing the regular expression over the string to find the
  // subsequent tags as RegExp only matches successfully one pattern at a time.
  // When there are no more successful matches it will return null.
  var matches = regexp.exec(string);
  while (matches !== null) {
    // matches[0] == entire match including tag
    // matches[1] == only the portion of the match inside the ()
    assert(matches[1], expectedValue);
    matches = regexp.exec(string);
  }
};

